package hu.bme.gabor.onlab.widget;

import hu.bme.gabor.onlab.Communication;
import hu.bme.gabor.onlab.R;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class TempWidget extends AppWidgetProvider {
	
	public static RemoteViews view = null;
	public static ComponentName thisWidget;
	private Communication comm = new Communication();
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		final int N = appWidgetIds.length;
		thisWidget = new ComponentName(context, TempWidget.class);
  
        for (int i=0; i<N; i++) {
            view = new RemoteViews(context.getPackageName(), R.layout.widget);
            view.setOnClickPendingIntent(R.id.button, getPendingSelfIntent(context));
            appWidgetManager.updateAppWidget(appWidgetIds[i], view);
		}
	}
	
	//@Override
    public void onReceive(Context context, Intent intent) {
    	super.onReceive(context, intent);
    	
    	if(view!=null){
    		view.setTextViewText(R.id.widget1label, comm.getDataFromAvr(1, 0));
    		AppWidgetManager manager = AppWidgetManager.getInstance(context);
    		manager.updateAppWidget(thisWidget, view);
    	}
    }
    
    protected PendingIntent getPendingSelfIntent(Context context) {
    	Log.d("myapp_widget", "selfcucc");
        Intent intent = new Intent(context, getClass());
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }
}